package appapi.service.impl;

import appapi.Request.ParkPageReq;
import appapi.Request.bigscreen.CarInOutReq;
import appapi.Vo.brider.AddPark;
import appapi.Vo.brider.ParkVO;
import appapi.common.ResponseBean;
import appapi.dao.ProjectMapper;
import appapi.dto.bigScreenDto.*;
import appapi.entity.Zhongjiao.ProjectDto;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.ParkCarInfo;
import appapi.entity.bridge.ParkControl;
import appapi.mapper.ParkCarInfoMapper;
import appapi.mapper.ParkMapper;
import appapi.service.ParkService;
import appapi.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hlm
 * @date
 * @description: 闸机设备
 */
@Service
public class ParkServiceImpl extends ServiceImpl<ParkMapper, ParkControl> implements ParkService {

   @Resource
   private ParkMapper parkMapper;

   @Resource
   private ProjectMapper projectMapper;

   @Resource
   private ParkCarInfoMapper parkCarInfoMapper;

    /**
     * 分页查询闸机设备列表
     *
     * @return
     */
    @Override
    public PageInfo<ParkVO> getParkByPage(ParkPageReq req) {
        PageInfo<ParkVO> pageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize())
                .doSelectPageInfo(() -> parkMapper.selectParkListByPage(req));

        return pageInfo;
    }

    /**
     * 新增
     *
     * @return
     */
    @Override
    public ResponseBean insertPark(AddPark addPark , UserInfo userInfo) {
        //判断闸机是否存在
        LambdaQueryWrapper<ParkControl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkControl::getProjId , addPark.getProjId());
        wrapper.eq(ParkControl::getParkName , addPark.getParkName());
        wrapper.eq(ParkControl::getParkNo , addPark.getParkNo());
        int count = parkMapper.selectCount(wrapper);
        if(count > 0){
            return ResponseBean.fail("该闸机已存在！");
        }
        ParkControl parkControl = BeanCopyUtils.copyBean(addPark , ParkControl.class);
        parkControl.setCreateTime(LocalDateTime.now());
        parkControl.setCreateUser(userInfo.getUserid());
        parkControl.setLastModifyUser(userInfo.getUserid());
        parkControl.setLastModifyTime(LocalDateTime.now());
        parkMapper.insert(parkControl);
        return ResponseBean.success();
    }


    /**
     * 删除
     *
     * @return
     */
    @Override
    public void delPark(Long parkId) {
        parkMapper.deleteById(parkId);
        //删除对应的闸机车辆
        ParkCarInfo parkCarInfo = new ParkCarInfo();
        parkCarInfo.setAuthFlag(0);
        LambdaQueryWrapper<ParkCarInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkCarInfo::getParkId , parkId);
        parkCarInfoMapper.update(parkCarInfo , wrapper);
    }


    /**
     * 详情
     *
     * @return
     */
    @Override
    public ResponseBean<ParkVO> getParkModel(Long parkId) {
        ParkControl parkControl = parkMapper.selectById(parkId);
        ParkVO parkVO = BeanCopyUtils.copyBean(parkControl , ParkVO.class);
        //封装项目信息
        ProjectDto projectDto = projectMapper.selectByPrimaryKey(parkVO.getProjId());
        if (projectDto != null) {
            parkVO.setProjName(projectDto.getProjName());
        }
        return ResponseBean.success(parkVO);
    }

    /**
     * 车辆进出统计（年/月/周）
     *
     * @return
     */
    @Override
    public CarInOutDto getCarInOutStatic(CarInOutReq req)
    {
        CarInOutDto dto= parkMapper.selectCarInOutStatic(req);
        if(dto!=null)
        {
            List<CarDayInOutDto> dayStatic=parkMapper.selectCarDayInOutStatic(req);
            dto.setDayInOut(dayStatic);
        }
       return dto;
    }
    /**
     * 车辆进出统计（近一周每日）
     *
     * @return
     */
    @Override
    public List<CarDayInOutDto> getCarDayInOutStatic(CarInOutReq req)
    {
        return parkMapper.selectCarDayInOutStatic(req);
    }

    /**
     * 获取车辆统计
     * @param req
     * @return
     */
    @Override
    public CarStaticDto getCarStatic(CarInOutReq req)
    {
        return parkMapper.selectCarStatic(req);
    }

    /**
     * 人员车辆统计
     * @param req
     * @return
     */
    @Override
    public PersonStaticDto getPersonCarStatic(CarInOutReq req)
    {
        PersonStaticDto dto =new PersonStaticDto();
        List<PersonCarStaticDto> carStaticDtoList=parkMapper.selectPersonCarStatic(req);
        dto.setCarStatic(carStaticDtoList);
        Integer backCount= parkMapper.selectCarBlackStatic(req);
        dto.setBlackount(backCount);
        return  dto;
    }
    /**
     * 车辆过夜统计
     * @param req
     * @return
     */
    @Override
    public CarOvernightDto getOvernightStatic(CarInOutReq req)
    {
        return parkMapper.selectCarOvernightStatic(req);
   }

}